package com.kkb.service.impl;

import com.kkb.common.utils.Constants;
import com.kkb.common.utils.StringUtils;
import com.kkb.mapper.TbCategoryMapper;
import com.kkb.pojo.TbCategory;
import com.kkb.pojo.TbCategoryExample;
import com.kkb.pojo.Ztree;
import com.kkb.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商品分类列表实现类
 **/
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private TbCategoryMapper categoryMapper;

    /**
     * 商品分类列表
     * @return 商品分类
     */
    @Override
    public List<TbCategory> getCategory() {
        TbCategoryExample example = new TbCategoryExample();
        List<TbCategory> categories = categoryMapper.selectByExample(example);
        return categories;
    }

    @Override
    public List<TbCategory> selectCategoryByLevel(Integer level) {
        TbCategoryExample example = new TbCategoryExample();
        TbCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo(level);
       return categoryMapper.selectByExample(example);
    }

    /**
     * 根据分类ID查询信息
     *
     * @param categoryId 分类ID
     * @return 分类信息
     */
    @Override
    public TbCategory selectCategoryById(Integer categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    /**
     * 根据ID查询分类信息是否存在
     *
     * @param categoryId 分类ID
     * @return 是否存在
     */
    @Override
    public String checkDeptNameUnique(Integer categoryId) {
        TbCategoryExample example = new TbCategoryExample();
        TbCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        long count = categoryMapper.countByExample(example);
        if (count>0){
            return Constants.CATEGORY_NAME_NOT_UNIQUE;
        }
        return Constants.CATEGORY_NAME_UNIQUE;
    }

    /**
     * 根据分类名称查询
     *
     * @param categoryName 分类名称
     * @return 分类信息
     */
    @Override
    public String checkDeptNameUnique(String categoryName) {
        TbCategoryExample example = new TbCategoryExample();
        TbCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryNameEqualTo(categoryName);
        long count = categoryMapper.countByExample(example);
        if (count>0){
            return Constants.CATEGORY_NAME_NOT_UNIQUE;
        }
        return Constants.CATEGORY_NAME_UNIQUE;
    }

    /**
     * 新增保存分类信息
     *
     * @param category 分类信息
     * @return 结果
     */
    @Override
    public int insertCategory(TbCategory category) {
        //创建时间为当前时间
        category.setCreateTime(new Date());
        return categoryMapper.insert(category);
    }

    /**
     * 修改保存分类
     *
     * @param category 分类信息
     * @return 结果
     */
    @Override
    public int updateCategory(TbCategory category) {
        category.setUpdateTime(new Date());
        return categoryMapper.updateByPrimaryKeySelective(category);
    }


    /**
     * 删除商品分类信息
     *
     * @param categoryId 分类ID
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Integer categoryId) {
        return categoryMapper.deleteByPrimaryKey(categoryId);
    }

    /**
     * 查询下级分类个数
     *
     * @param categoryId 上级ID
     * @return 分类信息
     */
    @Override
    public Long selectCount(Integer categoryId) {
        TbCategoryExample example = new TbCategoryExample();
        TbCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(categoryId);
        return categoryMapper.countByExample(example);
    }

    @Override
    public List<Ztree> selectCategoryTree(TbCategory tbCategory) {
        TbCategoryExample example = new TbCategoryExample();
        List<TbCategory> categories = categoryMapper.selectByExample(example);
        List<Ztree> ztrees = initZtree(categories);
        return ztrees;
    }

    /**
     * 对象转分类树
     *
     * @param categoryList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<TbCategory> categoryList) {

        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (TbCategory category : categoryList) {
                Ztree ztree = new Ztree();
                ztree.setId(category.getCategoryId());
                ztree.setpId(category.getParentId());
                ztree.setName(category.getCategoryName());
                ztree.setTitle(category.getCategoryName());
                ztrees.add(ztree);
        }
        return ztrees;
    }

}
